package com.example.back.controller;

import com.example.back.bean.UserSign;
import com.example.back.bean.UserSignReward;
import com.example.back.dao.UserDao;
import com.example.back.dao.UserSignDao;
import com.example.back.dao.UserSignRewardDao;
import com.example.back.util.TimeUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Api(value = "已废弃",tags = "签到信息接口")
@RestController
@RequestMapping("/sign")
public class SignInController {

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserSignDao userSignDao;

    @Autowired
    private UserSignRewardDao userSignRewardDao;

    /*签到接口*/
    @ApiOperation(value = "用户签到接口,已废弃",httpMethod = "POST")
    @RequestMapping("/today")
    public Map<String, Object> sign(String userId, boolean flag){
        HashMap<String,Object> map = new HashMap<>();
        int ads=flag?0:1;
        UserSign userSign = userSignDao.getOne(Long.parseLong(userId));
        if(userSign!=null){
            boolean isSameDay = TimeUtil.isSameDate(userSign.getSignTime(),new Date());
            if(isSameDay){
                map.put("code",400);
                map.put("message","您今天的奖励已经领取");
                return map;
            }
        }
        int number = userSign==null?-1:userSign.getNumber();
        number = (number==-1||number==7)?1:number+1;
        UserSignReward userSignReward = userSignRewardDao.getOne(number);
        String reward = userSignReward.getReward();
        int gift=Integer.parseInt(reward);
        gift = flag?gift*3:gift;
        userDao.update(gift, Long.parseLong(userId));
        userSignDao.insert(Long.parseLong(userId),new Date(), String.valueOf(gift),number,ads,0);
        map.put("code",0);
        map.put("message","恭喜您,获取金币奖励"+gift);
        return map;
    }

    /*领取救济金接口*/
    @ApiOperation(value = "领取救济金接口,已废弃",httpMethod = "POST")
    @RequestMapping("/relief")
    public Map<String,Object> relief(String userId){
        HashMap<String,Object> map = new HashMap<>();
        long gift = 4000;
        UserSign userSign = userSignDao.getType1(Long.parseLong(userId));
        if(userSign==null){
            return getStringObjectMap(userId, map, gift);
        }
        boolean isSameDay = TimeUtil.isSameDate(userSign.getSignTime(),new Date());
        if(isSameDay){
            if(userSign.getNumber()<4){
                userDao.update(gift, Long.parseLong(userId));
                userSignDao.insert(Long.parseLong(userId),new Date(), String.valueOf(gift),userSign.getNumber()+1,1,1);
                map.put("code",0);
                map.put("message","今天第"+(userSign.getNumber()+1)+"次领取救济金成功,恭喜您获取"+gift+"金币奖励!");
                return map;
            }
            map.put("code",400);
            map.put("message","今天的领取次数已达上限,您可以尝试观看广告获取更多金币哦!");
            return map;
        }
        return getStringObjectMap(userId, map, gift);
    }

    private Map<String, Object> getStringObjectMap(String userId, HashMap<String, Object> map, long gift) {
        userDao.update(gift, Long.parseLong(userId));
        userSignDao.insert(Long.parseLong(userId),new Date(), String.valueOf(gift),1,1,1);
        map.put("code",0);
        map.put("message","今天第1次领取救济金成功,恭喜您获取"+gift+"金币奖励!");
        return map;
    }

    @RequestMapping("/ads")
    public Map<String ,Object> ads(String userId){
        Map<String ,Object> map = new HashMap<>();
        userDao.update(1888,Long.parseLong(userId));
        map.put("code",0);
        map.put("message","领取成功!");
        return map;
    }

}
